package com.ballmaze.maze;


import java.util.HashMap;

public class MazeAction {
    Integer playerId;
    Direction direction;

    public MazeAction(Integer userid, Direction direction) {
        this.playerId = userid;
        this.direction = direction;
    }

    public enum Direction {
        UP, DOWN, LEFT, RIGHT, STOP
    }

    public static class MazeActionBuilder {
        static MazeActionBuilder instance;
        HashMap<String, Direction> directionHashMap;

        public static MazeActionBuilder getInstance() {
            if (instance == null) {
                instance = new MazeActionBuilder();
            }
            return instance;
        }

        private MazeActionBuilder() {
            directionHashMap = new HashMap<>();
            directionHashMap.put("UP", Direction.UP);
            directionHashMap.put("up", Direction.UP);
            directionHashMap.put("DOWN", Direction.DOWN);
            directionHashMap.put("down", Direction.DOWN);
            directionHashMap.put("LEFT", Direction.LEFT);
            directionHashMap.put("left", Direction.LEFT);
            directionHashMap.put("RIGHT", Direction.RIGHT);
            directionHashMap.put("right", Direction.RIGHT);
            directionHashMap.put("STOP", Direction.STOP);
            directionHashMap.put("stop", Direction.STOP);
        }

        public MazeAction build(String userid, String message) {
            return build(Integer.valueOf(userid), message);
        }

        public MazeAction build(Integer userid, String message) {
            Direction direction = directionHashMap.get(message);
            if (direction != null) {
                return new MazeAction(userid, direction);
            } else {
                return new MazeAction(userid, Direction.STOP);
            }
        }
    }
}
